package org.lalib.controller.action;

import java.util.ArrayList;
import java.util.List;

import org.lalib.acquire.IControllerAction;
import org.lalib.controller.form.CFoodCategoryForm;
import org.lalib.controller.form.CFoodCategoryForm.CategoryBean;
import org.lalib.controller.service.CCategoryService;
import org.lalib.controller.service.CFoodService;
import org.lalib.entity.object.ECategory;
import org.lalib.entity.object.EFood;
import org.lalib.entity.object.EItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Scope("request")
@Controller("cFoodCategoryAction")
public class CFoodCategoryAction implements IControllerAction {

	@Autowired
	private CFoodCategoryForm cFoodCategoryForm;

	@Autowired
	private CFoodService cFoodService;

	@Autowired
	private CCategoryService cCategoryService;

	private static final long serialVersionUID = 1L;

	public String init(EItem item) {
		EFood food = (EFood) item;
		resetFormVariables(food);
		return "foodCategory";
	}

	public void addToFood() {
		List<CategoryBean> selectedCategoryBeans = cFoodCategoryForm.getExcludedCategories();
		EFood food = cFoodCategoryForm.getFood();
		food = cFoodService.getFoodById(food.getId());

		boolean check = false;
		for (CategoryBean categoryBean : selectedCategoryBeans) {
			if (categoryBean.isSelected()) {
				food.getCategories().add(categoryBean.getCategory());
				check = true;
			}
		}

		if (check) {
			cFoodService.update(food);
			resetFormVariables(food);
		}
	}

	public void removeFromFood() {
		List<CategoryBean> selectedCategoryBeans = cFoodCategoryForm.getUserCategories();
		EFood food = cFoodCategoryForm.getFood();
		food = cFoodService.getFoodById(food.getId());

		boolean check = false;
		for (CategoryBean categoryBean : selectedCategoryBeans) {
			if (categoryBean.isSelected()) {
				deleteTableCategoryForCategory(food, categoryBean.getCategory());
				check = true;
			}
		}

		if (check) {
			cFoodService.update(food);
			resetFormVariables(food);
		}
	}

	private void resetFormVariables(EFood food) {
		food = (EFood) cFoodService.getFoodById(food.getId());
		List<ECategory> userCategories = food.getCategories();
		List<ECategory> allUserCategories = cCategoryService.getAllFoodCategories();
		List<ECategory> filteredCategories = cCategoryService.filterCategories(userCategories, allUserCategories);

		List<CategoryBean> userBaseCategoryBeans = getCategoryBeansForGivenCategories(userCategories);
		List<CategoryBean> excludedBaseCategoryBeans = getCategoryBeansForGivenCategories(filteredCategories);

		cFoodCategoryForm.setFood(food);
		cFoodCategoryForm.setUserCategories(userBaseCategoryBeans);
		cFoodCategoryForm.setExcludedCategories(excludedBaseCategoryBeans);
		cFoodCategoryForm.setExcludedBaseCategories(filteredCategories);
		cFoodCategoryForm.setUserBaseCategories(userCategories);
	}

	private void deleteTableCategoryForCategory(EFood food, ECategory category) {
		int id = 0;
		boolean found = false;

		for (int i = 0; i < food.getCategories().size(); i++) {
			if (food.getCategories().get(i).getId().equals(category.getId())) {
				id = i;
				found = true;
			}
		}

		if (found)
			food.getCategories().remove(id);
	}

	private List<CategoryBean> getCategoryBeansForGivenCategories(List<ECategory> categories) {
		List<CategoryBean> categoryBeans = new ArrayList<>();
		for (ECategory category : categories)
			categoryBeans.add(cFoodCategoryForm.new CategoryBean(category));
		return categoryBeans;
	}
}